package kai.rating;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener {
	//static Elo elo;
	static TextView lefttext,righttext;
	static View left,right;
	static Button draw;
	static ImageView leftimg,rightimg;
	static TextView print;
	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		new Elo(this,getSharedPreferences("Elo",Activity.MODE_PRIVATE));
		print=(TextView) findViewById(R.id.print);
		
		left=findViewById(R.id.left);
		right=findViewById(R.id.right);
		lefttext=(TextView) findViewById(R.id.lefttext);
		righttext=(TextView) findViewById(R.id.righttext);
		draw=(Button) findViewById(R.id.draw);
		leftimg=(ImageView) findViewById(R.id.leftimg);
		rightimg=(ImageView) findViewById(R.id.rightimg);
		draw.setOnClickListener(this);
		left.setOnClickListener(this);
		right.setOnClickListener(this);
		
		Elo.Refresh();
		Fight();
		RefreshPrint();
	}
	@Override
	public void onClick(View v) {
		if(Elo.row<2)
			return;
		if(draw==v)
		{
			print.append("\nDraw");
			Elo.Win(0);
		}
		else if(left==v)
		{
			print.append("\nWin Left");
			Elo.Win(1);
		}
		else if(right==v)
		{
			print.append("\nWin Right");
			Elo.Win(2);
		}
		Fight();
		RefreshPrint();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(0,0,0,R.string.add).setIcon(android.R.drawable.ic_menu_add);
		menu.add(0,1,1,R.string.rank).setIcon(android.R.drawable.ic_menu_agenda);
		menu.add(0,2,2,R.string.option).setIcon(android.R.drawable.ic_menu_manage);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		Intent intent;
		switch(item.getItemId())
		{
		case 0://Add
			intent=new Intent(this,AddObject.class);
			startActivityForResult(intent, 0);
			break;
		case 1://Rank
			intent=new Intent(this,Rank.class);
			startActivityForResult(intent,0);
			break;
		case 2://Option
			intent=new Intent(this,Option.class);
			startActivityForResult(intent, 0);
			break;
		}
		return true;
	}
	@Override
	protected void onActivityResult(int requestcode,int resultcode,Intent intent)
	{
		super.onActivityResult(requestcode,resultcode,intent);
		if(resultcode!=RESULT_OK)
			return;
		Elo.Refresh();
		Fight();
		RefreshPrint();
	}
	
	private void Fight()
	{
		final Bitmap noimg=BitmapFactory.decodeResource(getResources(),R.drawable.noimg);
		if(Elo.Fight()==false)
		{
			lefttext.setText("Add");
			righttext.setText("List");
			draw.setText("Can't playing");
			leftimg.setImageBitmap(noimg);
			rightimg.setImageBitmap(noimg);
			return;
		}
		lefttext.setText(Elo.GetName(Elo.left));
		righttext.setText(Elo.GetName(Elo.right));
		draw.setText(R.string.draw);
		Bitmap bm_l,bm_r;
		bm_l=BitmapFactory.decodeFile(getFilesDir().getAbsolutePath()+"/"+Elo.object[Elo.left].id+".png");
		if(bm_l==null)
			bm_l=noimg;
		bm_r=BitmapFactory.decodeFile(getFilesDir().getAbsolutePath()+"/"+Elo.object[Elo.right].id+".png");
		if(bm_r==null)
			bm_r=noimg;
		leftimg.setImageBitmap(bm_l);
		rightimg.setImageBitmap(bm_r);
	}
	private void RefreshPrint()
	{
		print.setText("");
		print.append(Elo.Print()+"\n\n");
		if(Elo.row<2)
		{
			print.append(getString(R.string.two_player));
		}
	}
}